﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Controller_App
{
    /// <summary>
    /// Besondere Form für Anfragen der Zeichnerkennung
    /// INIT muss gecallt werden
    /// </summary>
    public partial class frm_Zeichenerkennung_Query : Form
    {
        private IVisual.I_Controller _ctrl;
        private string[] fonts = new string[] { "Arial", "Courier", "Tahoma", "Times New Roman", "Verdana" };

        public frm_Zeichenerkennung_Query()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Übergibt den Controller. Erforderlich!
        /// </summary>
        /// <param name="_ctrl">Controller-interface</param>
        public void INIT(IVisual.I_Controller _ctrl)
        {
            this._ctrl = _ctrl;
        }

        /// <summary>
        /// Initialisiert de Combo's mit Buchstaben und Fonts
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_Zeichenerkennung_Query_Load(object sender, EventArgs e)
        {
            // Buchstaben adden
            for (int i = 0; i < 26; i++)
            {
                this._cb_letter.Items.Add((char)((int)'A' + i));
            }

            // add all fonts
            for (int i = 0; i < this.fonts.Length; i++)
            {
                this._cb_font.Items.Add(fonts[i]);
                this._cb_font.Items.Add(string.Format("{0} - Italic", fonts[i]));
            }
            this._cb_font.SelectedIndex = 0;
            this._cb_letter.SelectedIndex = 0;
        }

        /// <summary>
        /// Handler um die erkennung durchzuführen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _b_draw_Click(object sender, EventArgs e)
        {
            int i, n, maxIndex = 0;

            // aktuelle rezeptoren-stati auslesen
            int[] state = this._ctrl.OCR_GET_Receptors().GetReceptorsState(this._pb_main.GetImage());

            // netzeingabe vorbereiten
            float[] input = new float[state.Length];

            for (i = 0; i < state.Length; i++)
                input[i] = (float)state[i] - 0.5f;

            // Computen
            float[] output = this._ctrl.Compute(input);

            // Größten ausgabewert finden
            float max = output[0];
            for (i = 1, n = output.Length; i < n; i++)
            {
                if (output[i] > max)
                {
                    max = output[i];
                    maxIndex = i;
                }
            }

            this._tb_outp.Text = string.Format("{0}", (char)((int)'A' + maxIndex));
        }

        /// <summary>
        /// Zeichnet bei Indexänderung (Font oder Letter) auf das PB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _cb_letter_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._pb_main.DrawLetter((char)((int)'A' + this._cb_letter.SelectedIndex), fonts[this._cb_font.SelectedIndex >> 1], 90, ((this._cb_font.SelectedIndex & 1) != 0));
        }
    }
}
